/* 
 * File:   Polygon.cpp
 * Author: lgr013
 * 
 * Created on 13 de septiembre de 2014, 14:57
 */

#include "Polygon.h"

Polygon::Polygon() {
}

Polygon::Polygon(ObjFactory *objFactory, bool flagStatic, float posX, float posY, int scale, int sides, float mass, float rotation) {
    SBSLog* myLogFile;
    myLogFile->getInstance(MY_LOG_MODE);

    if (scale <= 0){
        myLogFile->writeInLog(2, "[2] El radio asociado al poligono es invalido (Radio no positivo)\n");                
    }

    Dot *dot = new Dot(posX,posY,0,0);  // ancho y alto = 0  
    this->dot = dot;    
    b2Vec2 size(0,0);
    float sca = scale * 0.01;
    Object *object = objFactory->getObject(flagStatic, dot->dotPixelsToWorld() , &size, sca, mass, sides, rotation);
    this->object = object;        
    this->scale = scale;    // pixeles
    this->sides = sides;

    myLogFile->writeInLog(0, "[0] Se ha creado un poligono exitosamente\n");                

}

void Polygon::draw(Uint8 r, Uint8 g, Uint8 b, Uint8 a){
    SBSLog* myLogFile;
    myLogFile->getInstance(MY_LOG_MODE);

    Object *obj = this->object;
    float posX = obj->getBody()->GetPosition().x;
    float posY = obj->getBody()->GetPosition().y;
    this->dot->setPos(posX,posY);    
//    printf("ANGLE draw: %4.3f\n",this->getAngle());

    bool ok = obj->getSkin()->makePolygon(this->dot->dotWorldToPixels(), this->sides, this->scale, this->getAngle(), r, g, b, a);

    if (!ok){
        myLogFile->writeInLog(1, "[1] Ha ocurrido un error mientras se intentaba dibujar el poligono\n");                    
    }
    
}

b2Vec2 Polygon::getPosition(){
    return this->object->getPosition();
}

float Polygon::getAngle(){
    return this->object->getAngle();
}

float Polygon::getAngularVelocity(){
    return this->object->getAngularVelocity();
}

void Polygon::setAngularVelocity(float velocity){
    this->object->setAngularVelocity(velocity);
}

Polygon::~Polygon() {
    delete this->object;
    delete this->dot;
//    printf("destructor Polygon \n");
}

